package com.leftshine.dnstest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.leftshine.dnstest.NetworkUtils.ApplicationUtil;
import com.leftshine.dnstest.NetworkUtils.ConnectivityUtil;
import com.leftshine.dnstest.NetworkUtils.DeviceUtil;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "DnsTest";
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);
        initView();
    }

    private void showAppInfo() {
        String appInfo = ApplicationUtil.getAppInfo(mContext);
        Log.i(TAG, appInfo);
        TextView tvAuth = findViewById(R.id.tvAuth);
        tvAuth.setText(appInfo);
    }

    private void initView() {
        final EditText editDomainName = findViewById(R.id.edDomainName);
        Button btnResolve = findViewById(R.id.btnResolve);
        final TextView tvResult = findViewById(R.id.tvResult);

        //editDomainName.setText("itms.scmcc.com.cn");
        //editDomainName.setText("STB.hb.chinamobile.com");
        editDomainName.setText("ipw.cn");
        btnResolve.requestFocus();
        showAppInfo();
        showDeviceInfo();
        //showDeviceInfo();

        btnResolve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvResult.setText("");
                String domainName = editDomainName.getText().toString();

                final AsyncTask<String, Void, String> execute = new AsyncTask<String, Void, String>() {
                    @Override
                    protected String doInBackground(String... params) {
                        String result = null;
                        try {
                            String name = params[0];
                            Log.d(TAG, "Domain Name: " + name);
                            // 解析域名对应的IP地址
                            InetAddress[] inetAddress = InetAddress.getAllByName(params[0]);
                            result = Arrays.toString(inetAddress);
                            Log.d(TAG, "Resolved IP: " + result);
                            result = result.replace(", ", "\n").replace("[", "").replace("]", "");
                        } catch (UnknownHostException e) {
                            Log.e(TAG, "Failed to resolve IP:", e);
                            e.printStackTrace();
                            result = e.toString();
                        }
                        return result;
                    }

                    @Override
                    protected void onPostExecute(String result) {
                        // TODO Auto-generated method stub
                        super.onPostExecute(result);
                        result = "DNS Servers: " + ConnectivityUtil.DnsUtil.getDns(mContext) + "\n解析结果：\n"+ result;
                        Log.d(TAG, "result: "+result);
                        tvResult.setText(result);
                        showDeviceInfo();
                    }
                }.execute(domainName);
            }
        });
    }

    private void showDeviceInfo() {
        String deviceInfo = DeviceUtil.getDeviceInfo(mContext);
        Log.d(TAG, "showDeviceInfo: "+deviceInfo);
        String netInfo = ConnectivityUtil.getNetworkInfo(this);
        Log.d(TAG, "showNetworkInfo: " + netInfo);

        TextView tvDevInfo = findViewById(R.id.tvDevInfo);
        tvDevInfo.setText(deviceInfo+"\n\n"+netInfo);
    }

}
